<template>
  <div class="major-event-list-content">
    <div class="list-title m5">
      事件列表
      <span>{{eventlist.length}}</span>
    </div>
    <div class="list-wrap" ref="listWrap">
      <div class="list-item" v-for="(item,index) in eventlist" :key="index" @click="detailOpen(item)">
        <p class="title">{{item.eventName}}</p>
        <div class="flex items-end justify-between">
          <div class="info">
            <p class="time">事发时间： {{item.eventTime}}</p>
            <p class="address">事发位置： {{item.location}}</p>
          </div>
          <div class="icon-zhankai" :class="{'shouqi':item.isShow}" @click.stop="showDetail(item,index)" >
            <img src="@/assets/icon/icon-unfold.png" alt="">
          </div>
        </div>
        <div class="detail" v-if="item.isShow&&item.detailData">
          <div class="desc">
            {{item.detailData.sjnr}}
          </div>
          <div class="images">
            <img v-for="(img,id) in item.detailData.imageList" :key="id" :src="img" @click.stop="previewImg(item.detailData.imageList)" alt="">
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref, reactive, toRefs, watch, onActivated, onMounted,watchEffect } from 'vue'
import { showImagePreview } from 'vant'
import EventServe from '@/api/event'

defineOptions({
  name: 'MajorEventList'
})

const props = defineProps({
  list: {
    type: Array,
    default: []
  }
})

const emit = defineEmits(['detail-link'])

const viewportHeight = ref(0);

watchEffect(() => {
  let height = window.innerHeight || document.documentElement.clientHeight
  viewportHeight.value = height - 350;
});

watch(
  () => props.list,
  (val) => {
    if(val.length>0){
      eventlist.value = val;
    }
  }
);

const detailOpen = (data: Object) => {
  if(data.detailData)
    emit('detail-link', data.detailData)
  else
    getDetail(data,'emit')
}

const detailData = ref({})
//获取事件详情
const getDetail = (item: Object,type: string) => {
  EventServe.getEventsInfoById(item.id).then(res=>{
    item.detailData = res.data
    if(type=='emit') emit('detail-link', item.detailData)
  })
}

//图片放大查看
const previewImg = (list) => {
  showImagePreview({
    images: list,
    startPosition: 1
  });
}

const eventlist = ref([])
const activeIndex = ref(null)

const showDetail = (item:Object,index:Number) =>{
  if(activeIndex.value != index){
    eventlist.value.forEach(ele=>{
      ele.isShow = false;
    })
  }
  item.isShow = !item.isShow;
  activeIndex.value = index;
  if(item.isShow){
    getDetail(item)
  }
}
</script>

<script lang="ts">

</script>

<style lang="less" scoped>
.major-event-list-content{
  border-radius: 10px 10px 0 0;
  background: #FFFFFF;
  color: #212121;
  box-shadow: 0px 3px 10px 7px #00000010;
  position: fixed;
  left: 0px;
  right: 0px;
  bottom: 0px;
  z-index: 9;
  .list-title{
    color: #000;
    font-weight: bold;
    font-size: 16px;
    padding: 18px 0 15px;
    border-bottom: 1px solid #eee;
    text-align: center;
  }
  .list-wrap{
    max-height: calc(100vh - 260px);
    overflow: auto;
    padding: 0 15px 10px;
  }
  .list-item{
    padding: 8px 0 0;
    border-bottom: 1px solid #eee;
    &:last-child{
      border: none;
    }
    .title{
      color: #0E0D16;
      font-weight: bold;
      font-size: 16px;
      line-height: 20px;
    }
    .info{
      border-radius: 6px;
      padding: 6px 9px 7px 0px;
      font-size: 12px;
      line-height: 22px;
      .time{
        background: url("@/assets/icon/icon-blue-time.png") no-repeat left center;
        background-size: 12px 12px;
        padding-left: 15px;
      }
      .address{
        background: url("@/assets/icon/icon-blue-position.png") no-repeat left 4px;
        background-size: 12px 12px;
        padding-left: 15px;
      }
    }
    .detail{
      padding-bottom: 10px;
    }
    .desc{
      font-size: 14px;
      line-height: 22px;
    }
    .images{
      margin-top: 7px;
      display: flex;
      flex-wrap: wrap;
      img{
        border-radius: 5px;
        width: 72px;
        height: 72px;
        margin: 0 7px 7px 0;
        &:nth-child(4n){
            margin-right: 0;
        }
      }
    }
    .icon-zhankai{
      margin-bottom: 10px;
      transition: all 0.3s;
      img{
        width: 24px;
        height: 24px;
      }
      &.shouqi{
        transform: rotate(-180deg);
      }
    }
  }
}
</style>

